package com.uplink.msmq.util;

import net.sf.json.JSONObject;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class MapUtil {

    private static ConcurrentHashMap concurrentHashMap = new ConcurrentHashMap();

    private MapUtil() {
    }

    public static ConcurrentHashMap getInstance() {
        return concurrentHashMap;
    }

    public static List removeDuplicate(List list) {
        LinkedHashSet<String> set = new LinkedHashSet<String>(list.size());
        set.addAll(list);
        list.clear();
        list.addAll(set);
        return list;
    }

    /**
     * json string 转换为 map 对象
     *
     * @param jsonObj
     * @return
     */
    public static Map jsonToMap(Object jsonObj) {
        JSONObject jsonObject = JSONObject.fromObject(jsonObj);
        Map<String, Object> map = (Map) jsonObject;
        return map;
    }

    /**
     * json string 转换为 对象
     *
     * @param jsonObj
     * @param type
     * @return
     */
    public static <T> T jsonToBean(Object jsonObj, Class<T> type) {
        JSONObject jsonObject = JSONObject.fromObject(jsonObj);
        T obj = (T) JSONObject.toBean(jsonObject, type);
        return obj;
    }
}
